

package at.fhj.itm.metadata.impl.postgres;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.xml.ws.ProtocolException;

/**
 *
 * @author Seuchter
 */
public class PostgresJdbcUtil {

    private PostgresJdbcUtil(){

    }

    
    protected static String quoteDatabaseItem(String item){
    	//remove all quotes if the db name is partially quoted i.e. "f-oo".bar
    	item = item.replace("\"", "");
    	StringBuilder sb = new StringBuilder();
    	String [] name = item.split("\\.");
    	if(name.length == 0){
    		name = new String[]{item};
    	}
    	for(int i = 0; i < name.length; i++){
    		sb.append("\"").append(name[i]).append("\"");
    	
    		if(i < (name.length - 1)){
    			sb.append(".");
    		}
    	}
    
    	return sb.toString();
    }
    
    protected static Integer[] getInt2Vector(ResultSet resultSet, String field) throws SQLException
    {
        String int2VecRaw  = resultSet.getString(field);

        String [] rawElements = int2VecRaw.split(" ");
        Integer[] intArray = new Integer[rawElements.length];
        int curIndex = 0;
        for(String elem : rawElements)
        {
            int curVal =  Integer.parseInt(elem);
            intArray[curIndex] = curVal;
            curIndex++;
        }
        return intArray;


    }


    protected static Long[] getOidVector(ResultSet resultSet, String field) throws SQLException{
                String int2VecRaw  = resultSet.getString(field);

        String [] rawElements = int2VecRaw.split(" ");
        Long[] longArray = new Long[rawElements.length];
        int curIndex = 0;
        for(String elem : rawElements)
        {
            long curVal =  Long.parseLong(elem);
            longArray[curIndex] = curVal;
            curIndex++;
        }
        return longArray;
    }

}
